package com.xinhe.web.redis;

import com.xinhe.web.vo.ValueConfigView;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class ValueConfigCacheRedis {

    private static final String REDIS_KEY = "value-config";

    @Autowired
    private ObjectRedisTemplate<String, ValueConfigView> redisTemplate;

    public ValueConfigView get(String id) {
        HashOperations<String, String, ValueConfigView> opsForHash = this.redisTemplate.opsForHash();
        ValueConfigView valueConfigView = (ValueConfigView)opsForHash.get("value-config", id);
        if (null == valueConfigView) {
            log.warn("{}未配置", id);
        }
        return valueConfigView;
    }

    public String getValue(String id) {
        return getValue(id, "");
    }

    public String getValue(String id, String defaultValue) {
        ValueConfigView view = get(id);
        if (null == view || StringUtils.isBlank(view.getValue())) {
            return defaultValue;
        }
        return view.getValue();
    }

    public Integer getInteger(String id) {
        return getInteger(id, null);
    }

    public Integer getInteger(String id, Integer defaultValue) {
        String value = getValue(id);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Integer.valueOf(value);
    }

    public Long getLong(String id) {
        return getLong(id, null);
    }

    public Long getLong(String id, Long defaultValue) {
        String value = getValue(id);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Long.valueOf(Long.parseLong(value));
    }

    public Double getDouble(String id) {
        return getDouble(id, null);
    }

    public Double getDouble(String id, Double defaultValue) {
        String value = getValue(id);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Double.valueOf(value);
    }

    public Float getFloat(String id) {
        return getFloat(id, null);
    }

    public Float getFloat(String id, Float defaultValue) {
        String value = getValue(id);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Float.valueOf(value);
    }

    public BigDecimal getBigDecimal(String id) {
        return getBigDecimal(id, null);
    }

    public BigDecimal getBigDecimal(String id, BigDecimal defaultValue) {
        String value = getValue(id);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return new BigDecimal(value);
    }

    public void init(List<ValueConfigView> valueConfigs) {
        HashOperations<String, String, ValueConfigView> opsForHash = this.redisTemplate.opsForHash();
        Map<String, ValueConfigView> map = new HashMap<>();
        for (ValueConfigView valueConfig : valueConfigs) {
            map.put(valueConfig.getId(), valueConfig);
        }
        opsForHash.putAll("value-config", map);
    }

    public void put(ValueConfigView valueConfig) {
        HashOperations<String, String, ValueConfigView> opsForHash = this.redisTemplate.opsForHash();
        opsForHash.put("value-config", valueConfig.getId(), valueConfig);
    }

    public void delete(String id) {
        HashOperations<String, String, ValueConfigView> opsForHash = this.redisTemplate.opsForHash();
        opsForHash.delete("value-config", new Object[] { id });
    }
}
